package com.haoshuang.zhouzhoubang.ui.page.study.detail

import android.content.Context
import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import com.haoshuang.zhouzhoubang.databinding.ActivityTrainDetailsBinding
import com.haoshuang.zhouzhoubang.ui.page.study.exam.ExamActivity
import com.haoshuang.zhouzhoubang.utils.WebSettingsUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visibleOrGone


/**
 *  学习培训详情页
 */
class TrainDetailsActivity : BaseVMActivity<TrainDetailsViewModel, ActivityTrainDetailsBinding>() {

    companion object {
        fun launch(context: Context, type: Int?, articleId: Int?, url: String?, isStudy: Boolean = false){
            context.startPage<TrainDetailsActivity>(
                "url" to url,
                "type" to type,
                "articleId" to articleId,
                "isStudy" to isStudy
            )
        }
    }


    private var url = ""
    private var type = 1
    private var articleId = -1
    // 是否需要学习, 需要考试, 复习不用考试
    private var isStudy = false

    override fun initIntent() {
        isStudy = intent.getBooleanExtra("isStudy", false)
        url = intent.getStringExtra("url") ?: ""
        type = intent.getIntExtra("type", 1)
        articleId = intent.getIntExtra("articleId", -1)
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "详情"
        binding.titleBar.leftImageView().click { finish() }

        WebSettingsUtils.setWebSettings(mActivity, binding.webView)

        binding.webView.loadUrl(url)
        binding.tvStartExam.visibleOrGone(type == 2)

        binding.tvStartExam.clickWithTrigger {
            ExamActivity.launch(mActivity, 1, articleId)
        }

        binding.tvStartExam.visibleOrGone(isStudy)

    }

    override fun initData() {
    }

    override fun startObserve() {
    }

    override fun onDestroy() {
        binding.webView.destroy()
        super.onDestroy()
    }

}